/*
@auther fenglei
*/
package xxdb

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"time"
)

type RedisOptions struct {
	DSN string
}

func NewRedis(opts *RedisOptions) (*redis.Client, error) {
	if opts.DSN == "" {
		return nil, fmt.Errorf("DNS is nil")
	}
	option := &redis.Options{
		Addr:        opts.DSN,
		Password:    "",
		DB:          0,
		DialTimeout: 5 * time.Second,
	}
	client := redis.NewClient(option)
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	if err := client.Ping(ctx).Err(); err != nil {
		return nil, fmt.Errorf("Redis failed to connect to %s: %v", opts.DSN, err)
	}
	return client, nil
}
func CloseRedis(redis *redis.Client) error {
	return redis.Close()
}
